﻿/**
 * @file std_comm.inl
 * Copyright (c) 2019 Gaaagaa. All rights reserved.
 * 
 * @author  : Gaaagaa
 * @date    : 2019-09-09
 * @version : 1.0.0.0
 * @brief   : 定义内部 公共 的相关数据类型、常量、宏 等。
 */

/**
 * The MIT License (MIT)
 * Copyright (c) Gaaagaa. All rights reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#ifndef __STD_COMM_INL__
#define __STD_COMM_INL__

#ifndef __STD_COMM_H__
#error "Please include std_comm.h before this file."
#endif // __STD_COMM_H__

////////////////////////////////////////////////////////////////////////////////

#define XSTD_ECTXT(xstd_this)   ((xstd_context_t *)&(xstd_this)->xstd_ectxt)
#define XSTD_ETYPE(xstd_this)   (XSTD_ECTXT(xstd_this)->xstd_etype)
#define XSTD_ESIZE(xstd_this)   (XSTD_ECTXT(xstd_this)->xstd_esize)
#define XSTD_ALTOR(xstd_this)   (XSTD_ECTXT(xstd_this)->xstd_altor)

#define XSTD_MALLOC(xstd_this, xst_size)  \
    XSTD_ALTOR(xstd_this)->xfunc_allocate(xst_size)

#define XSTD_DEALLOC(xstd_this, xmt_mptr) \
    XSTD_ALTOR(xstd_this)->xfunc_dealloc(xmt_mptr)

#define XSTD_ALFROM(xstd_this, xet_dptr, xet_sptr) \
    XSTD_ALTOR(xstd_this)->xfunc_copyfrom(         \
        (xet_dptr), (xet_sptr), XSTD_ECTXT(xstd_this))

#define XSTD_ALMOVE(xstd_this, xet_dptr, xet_sptr) \
    XSTD_ALTOR(xstd_this)->xfunc_copymove(         \
        (xet_dptr), (xet_sptr), XSTD_ECTXT(xstd_this))

#define XSTD_ALDEST(xstd_this, xet_elem)   \
    XSTD_ALTOR(xstd_this)->xfunc_destruct( \
        (xet_elem), XSTD_ECTXT(xstd_this))

#define XSTD_ISSAME_TYPE(xstd_lthis, xstd_rthis)            \
    (((xstd_lthis) == (xstd_rthis)) ||                      \
     ((XSTD_ETYPE(xstd_lthis) == XSTD_ETYPE(xstd_rthis)) && \
      (XSTD_ESIZE(xstd_lthis) == XSTD_ESIZE(xstd_rthis)) && \
      (XSTD_ALTOR(xstd_lthis) == XSTD_ALTOR(xstd_rthis))))

#define XSTD_ALTOR_NCREF(xstd_altor) \
    ((xstd_altor_t)((XSTD_NULL != (xstd_altor)) ? (xstd_altor) : &xstd_altor_comm))

#define XSTD_ECTXT_UPDATE(xstd_this, xstd_ctxt)                         \
    do                                                                  \
    {                                                                   \
        (xstd_this)->xstd_ectxt.xstd_etype = (xstd_ctxt)->xstd_etype;   \
        (xstd_this)->xstd_ectxt.xstd_esize = (xstd_ctxt)->xstd_esize;   \
        (xstd_this)->xstd_ectxt.xstd_owner = (xstd_owner_t)(xstd_this); \
        (xstd_this)->xstd_ectxt.xstd_altor =                            \
            XSTD_ALTOR_NCREF((xstd_ctxt)->xstd_altor);                  \
    } while (0)

////////////////////////////////////////////////////////////////////////////////

#endif // __STD_COMM_INL__

